package com.ext.aic.box.wx;

import android.text.TextUtils;
import android.view.accessibility.AccessibilityNodeInfo;

import com.ext.aic.AICManager;
import com.ext.aic.beans.box.ChatViewElement;
import com.ext.aic.beans.box.ChatWindowElement;
import com.ext.aic.beans.box.ConversationElement;
import com.ext.aic.beans.box.ConversationViewElement;
import com.ext.aic.beans.box.ConversationWindowElement;
import com.ext.aic.beans.box.ViewElement;
import com.ext.aic.utils.AccessibilityNodeUtils;
import com.ext.aic.utils.WxElementUtils;
import com.eyinfo.android_pure_utils.snow.Butterfly;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;

import java.util.List;

public class WxViews {
    private WxDbHelper dbHelper = new WxDbHelper();
    private ConversationWindowElement conversationWindowElement;

    //region 会话列表相关方法
    public boolean hasConversationWindow(AccessibilityNodeInfo rootNode) {
        ConversationViewElement conversationWindowElements = WxElementUtils.getInstance().getConversationWindowElements();
        List<ViewElement> elements = conversationWindowElements.getListTopViewIds();
        for (ViewElement m : elements) {
            if (TextUtils.isEmpty(m.getId()) || TextUtils.isEmpty(m.getTitleId())) {
                continue;
            }
            List<AccessibilityNodeInfo> nodes = rootNode.findAccessibilityNodeInfosByViewId(m.getId());
            if (ObjectJudge.isNullOrEmpty(nodes)) {
                continue;
            }
            AccessibilityNodeInfo nodeInfo = nodes.get(0);
            List<AccessibilityNodeInfo> childNodes = nodeInfo.findAccessibilityNodeInfosByViewId(m.getTitleId());
            if (!ObjectJudge.isNullOrEmpty(childNodes)) {
                dbHelper.saveCurrentVersion(m.getVersion());
                return true;
            }
        }
        return false;
    }

    public ConversationWindowElement findConversationWindowElement(AccessibilityNodeInfo rootNode) {
        if (conversationWindowElement != null && conversationWindowElement.getId() != 0 && !TextUtils.isEmpty(conversationWindowElement.getWindowId()) && !TextUtils.isEmpty(conversationWindowElement.getListId())) {
            //临时缓存存在直接返回
            return conversationWindowElement;
        }
        conversationWindowElement = dbHelper.findConversationWindowElement(dbHelper.getCurrentVersion());
        if (conversationWindowElement != null && conversationWindowElement.getId() != 0 && !TextUtils.isEmpty(conversationWindowElement.getWindowId()) && !TextUtils.isEmpty(conversationWindowElement.getListId())) {
            //数据库找到记录也直接返回
            return conversationWindowElement;
        }
        ConversationViewElement conversationWindowElements = WxElementUtils.getInstance().getConversationWindowElements();
        List<ViewElement> conversationListTopViewElements = conversationWindowElements.getListTopViewIds();
        ConversationWindowElement item = new ConversationWindowElement();
        for (ViewElement m : conversationListTopViewElements) {
            if (TextUtils.isEmpty(m.getId())) {
                continue;
            }
            List<AccessibilityNodeInfo> nodes = rootNode.findAccessibilityNodeInfosByViewId(m.getId());
            if (ObjectJudge.isNullOrEmpty(nodes)) {
                continue;
            }
            dbHelper.saveCurrentVersion(m.getVersion());
            item.setId(dbHelper.getCurrentVersion().hashCode());
            item.setVersion(dbHelper.getCurrentVersion());
            item.setWindowId(m.getId());
            item.setListId(AccessibilityNodeUtils.getInstance().verifyId(rootNode, conversationWindowElements.getListIds()));
            AICManager.getInstance().insertOrUpdate(ConversationWindowElement.class, item);
            break;
        }
        conversationWindowElement = item;
        return conversationWindowElement;
    }

    public String getNewMessageMarkId() {
        String newMessageMarkId = dbHelper.getNewMessageMarkId();
        if (!TextUtils.isEmpty(newMessageMarkId)) {
            return newMessageMarkId;
        }
        String version = dbHelper.getCurrentVersion();

        ConversationViewElement conversationWindowElements = WxElementUtils.getInstance().getConversationWindowElements();
        List<ViewElement> listItemNewMsgIds = conversationWindowElements.getListItemNewMsgIds();
        for (ViewElement m : listItemNewMsgIds) {
            if (!TextUtils.equals(version, m.getVersion())) {
                continue;
            }
            newMessageMarkId = m.getId();

            ConversationElement conversationElement = new ConversationElement();
            conversationElement.setId(Butterfly.getInstance().nextId());
            conversationElement.setVersion(m.getVersion());
            conversationElement.setNewMsgMarkId(newMessageMarkId);
            AICManager.getInstance().insertOrUpdate(ConversationElement.class, conversationElement);
            break;
        }
        return newMessageMarkId;
    }
    //endregion

    //region 聊天窗口相关方法
    public String getChatListTextViewId(AccessibilityNodeInfo messageNode, List<ViewElement> textViewIds) {
        return AccessibilityNodeUtils.getInstance().verifyId(messageNode, textViewIds);
    }

    public ChatWindowElement findChatWindowElement(AccessibilityNodeInfo rootNode) {
        ChatWindowElement chatWindowElement = dbHelper.findChatWindowElement(dbHelper.getCurrentVersion());
        if (chatWindowElement != null && chatWindowElement.getId() != 0 &&
                !TextUtils.isEmpty(chatWindowElement.getWindowId()) &&
                !TextUtils.isEmpty(chatWindowElement.getChatListViewId()) &&
                !TextUtils.isEmpty(chatWindowElement.getEditViewId())) {
            return chatWindowElement;
        }
        ChatViewElement chatViewElement = WxElementUtils.getInstance().getChatWindowElements();
        List<ViewElement> markViewIds = chatViewElement.getMarkViewIds();
        for (ViewElement m : markViewIds) {
            if (TextUtils.isEmpty(m.getId())) {
                continue;
            }
            List<AccessibilityNodeInfo> nodes = rootNode.findAccessibilityNodeInfosByViewId(m.getId());
            if (ObjectJudge.isNullOrEmpty(nodes)) {
                continue;
            }
            dbHelper.saveCurrentVersion(m.getVersion());
            AccessibilityNodeInfo container = nodes.get(0);

            chatWindowElement = new ChatWindowElement();
            chatWindowElement.setId(Butterfly.getInstance().nextId());
            chatWindowElement.setVersion(m.getVersion());
            chatWindowElement.setWindowId(m.getId());
            chatWindowElement.setChatListViewId(AccessibilityNodeUtils.getInstance().verifyId(rootNode, chatViewElement.getListViewIds()));
            chatWindowElement.setEditViewId(AccessibilityNodeUtils.getInstance().verifyId(container, chatViewElement.getEditViewIds()));
            AICManager.getInstance().insertOrUpdate(ChatWindowElement.class, chatWindowElement);
            break;
        }
        return chatWindowElement;
    }

    public boolean hasChatWindow(AccessibilityNodeInfo rootNode) {
        ChatWindowElement chatWindowElement = findChatWindowElement(rootNode);
        return chatWindowElement != null && !TextUtils.isEmpty(chatWindowElement.getWindowId());
    }
    //endregion
}
